#!/bin/bash

grep -v '^#' trans.map|grep -v '^$' >trans.map.clean

cat trans.map.clean|while read line 
do
	itrans=`echo $line|awk -F " " '{print $1}'`
	hk=`echo $line|awk -F " " '{print $2}'`
	slp1=`echo $line|awk -F " " '{print $3}'`
	vel=`echo $line|awk -F " " '{print $4}'`
	iast=`echo $line|awk -F " " '{print $5}'`
	iso15919=`echo $line|awk -F " " '{print $6}'`
	uni=`echo $line|awk -F " " '{print $7}'`
	echo \"$itrans\", >>itrans.arr
	echo \"$hk\", >>hk.arr
	echo \"$slp1\", >>slp1.arr
	echo \"$vel\", >>vel.arr
	echo \"$iast\", >>iast.arr
	echo \"$iso15919\", >>iso15919.arr
	echo $uni, >>uni.arr
done

itrans_map_begin="const char *itrans_map[] = {"
hk_map_begin="const char *hk_map[] = {"
slp1_map_begin="const char *slp1_map[] = {"
velthuis_map_begin="const char *velthuis_map[] = {"
iast_map_begin="const char *iast_map[] = {"
iso15919_map_begin="const char *iso15919_map[] = {"
uni_map_begin="const int uni_map[] = {"

all_end="};"
echo "#ifndef TRANSMAP_H" >transmap.h
echo "#define TRANSMAP_H" >>transmap.h
echo "//this file is generated by gentransheader.sh" >>transmap.h
echo "//do not edit it directly!!!!!!!!!!!!!!!!!!" >>transmap.h
echo "//if you need add contents, please edit gentransheader.sh and trans.map in transmap dir" >>transmap.h
echo "//0x001 in unicode map means L+h, since SLP1 had a special mark | for this combination character." >>transmap.h
echo "//I use 0x001 as a substitution and process it in itrans_to_deva function, if we find 0x001 from uni_map, replace it." >>transmap.h
echo >>transmap.h

echo $itrans_map_begin >>transmap.h
cat itrans.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h

echo $hk_map_begin >>transmap.h
cat hk.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h

echo $slp1_map_begin >>transmap.h
cat slp1.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h


echo $velthuis_map_begin >>transmap.h
cat vel.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h

echo $iast_map_begin >>transmap.h
cat iast.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h

echo $iso15919_map_begin >>transmap.h
cat iso15919.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h


echo $uni_map_begin >>transmap.h
cat uni.arr >>transmap.h
echo $all_end >>transmap.h
echo >>transmap.h
echo >>transmap.h
echo >>transmap.h

echo "#endif" >>transmap.h

sed -i 's#""#"\\"#g' transmap.h

rm -rf trans.map.clean
rm -rf *.arr
